package com.justerdu.mvpclient.user.model.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.justerdu.mvpclient.user.model.domain.User;
import com.justerdu.mvpclient.util.DBOpenHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author JusterDu 2016/11/21.
 * @Func
 */
public class UserDao implements IUserDao {

    private DBOpenHandler dbOpenHandler;

    public UserDao(Context context) {
        this.dbOpenHandler = new DBOpenHandler(context, "mvpsqlite.db", null, 1);
    }

    @Override
    public void save(User user) {
        SQLiteDatabase db = dbOpenHandler.getWritableDatabase();// 取得数据库操作
        db.execSQL("insert into tb_user (name, age, sex) values(?, ?, ?)", new Object[] { user.getName(), user.getAge(), user.getSex() });
        db.close();
    }

    @Override
    public void delete(Integer id) {
        SQLiteDatabase db = dbOpenHandler.getWritableDatabase();
        db.execSQL("delete from tb_user where id=?", new Object[] { id.toString() });
        db.close();
    }

    @Override
    public void update(User user) {
        SQLiteDatabase db = dbOpenHandler.getWritableDatabase();
        db.execSQL("update tb_user set name=?,age=?,sex=? where" + " id=?", new Object[] { user.getName(), user.getAge(), user.getSex(), user.getId() });
        db.close();
    }

    @Override
    public User findUserById(Integer id) {
        User user = null;
        SQLiteDatabase db = dbOpenHandler.getReadableDatabase();
        // 用游标Cursor接收从数据库检索到的数据
        Cursor cursor = db.rawQuery("select * from tb_user where id=?", new String[] { id.toString() });
        if (cursor.moveToFirst()) {// 依次取出数据
            user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndex("id")));
            user.setName(cursor.getString(cursor.getColumnIndex("name")));
            user.setAge(cursor.getInt(cursor.getColumnIndex("age")));
            user.setSex(cursor.getString(cursor.getColumnIndex("sex")));
        }
        db.close();
        return user;
    }

    @Override
    public List<User> findAll() {
        List<User> list = new ArrayList<User>();
        User user = null;
        SQLiteDatabase db = dbOpenHandler.getReadableDatabase();
        // Cursor cursor=db.rawQuery("select * from tb_user limit ?,?", new String[]{offset.toString(),maxLength.toString()});
        // //这里支持类型MYSQL的limit分页操作

        Cursor cursor = db.rawQuery("select * from tb_user ", null);
        while (cursor.moveToNext()) {
            user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndex("id")));
            user.setName(cursor.getString(cursor.getColumnIndex("name")));
            user.setAge(cursor.getInt(cursor.getColumnIndex("age")));
            user.setSex(cursor.getString(cursor.getColumnIndex("sex")));
            list.add(user);
        }
        db.close();
        return list;
    }

    @Override
    public long getCount() {
        SQLiteDatabase db = dbOpenHandler.getReadableDatabase();
        Cursor cursor = db.rawQuery("select count(*) from tb_user ", null);
        cursor.moveToFirst();
        db.close();
        return cursor.getLong(0);
    }
}
